/*
 * @Description: 
 * @Author: zhangwei
 * @Date: 2023-02-13 16:47:18
 * @LastEditTime: 2023-02-17 12:18:34
 * @LastEditors: zhangwei
 */

function getWordLen(str) {
	var jmz = {}
	jmz.GetLength = function(str) {
		return str.replace(/[\u0391-\uFFE5]/g, "aa").length //先把中文替换成两个字节的英文，在计算长度
	}
	return jmz.GetLength(str)
}

export const getTextWidth = (text, fontSize) => {
	var len = getWordLen(text)
	var size = parseInt(fontSize)
	if (size == 0) {
		size = 1
	}
	var width = len * size / 2

	return width
}



//json对象的深度克隆
export const deepClone = (origin) => {
	if (origin === undefined) {
		return undefined
	}
	return JSON.parse(JSON.stringify(origin))
}

//生成随机ID
export const generateId = () => {
	return 'xx4xxxyxxx'.replace(/[xy]/g, function(c) {
		var r = (Math.random() * 16) | 0,
			v = c == 'x' ? r : (r & 0x3) | 0x8
		return v.toString(16)
	})
}

//判断对象、字符串、数组是否为空
export const isEmpty = (source) => {
	if (source == null || source === undefined) {
		return true
	}

	if (source instanceof Array && source.length == 0) {
		return true
	}

	//判断是否是字符串
	if (typeof source == "string" && source.trim().length == 0) {
		return true
	}

	//判断是对象数组
	return false
}

//只获取json对象指定的key
export const filterJsonByKey = (jsonObj, keys) => {
	let json = {}
	for (let key in jsonObj) {
		if (keys.indexOf(key) > -1) {
			json[key] = jsonObj[key]
		}
	}
	return json
}

//json对象key移
export const copyJsonKey = (fromJson, toJson, keys) => {
	keys.forEach(key => {
		toJson[key] = fromJson[key]
	})
}

export const getWeek = (dateStr) => {
	const fixDateStr = dateStr.replaceAll('-', '/') // ios的时间格式要以/为分隔
	let _date = new Date(fixDateStr)
	// let day = _date.getDay() + 1
	let day = _date.getDay()
	let arrs = ["周天", "周一", "周二", "周三", "周四", "周五", "周六"]
	return arrs[day]
}

export const toMoney = (num, isFloat) => {
	if (num == 0) {
		return "￥0"
	}
    if (num) {
        if (isNaN(num)) {
            alert("金额中含有不能识别的字符")
            return
        }
        num = typeof num == "string" ? parseFloat(num) : num  //判断是否是字符串如果是字符串转成数字
        num = num.toFixed(2)  //保留两位
        // num = parseFloat(num)  //转成数字
        // num = num.toLocaleString()  //转成金额显示模式（android下不起作用）
		num = num.replace(/(\d{1,3})(?=(\d{3})+(?:$|))/g, "$1,")  // 转成金额显示模式（兼容android、ios）
        //判断是否有小数
        if (num.indexOf(".") == -1) {
            num = "￥" + num + ".00"
        } else {
            num = num.split(".")[1].length < 2 ? ("￥" + num + "0") : ("￥" + num)
        }
		if (isFloat !== undefined && !isFloat) {
			let tt =  num.substring(0, num.length - 3)
			return tt
		}
        return num  //返回的是字符串23,245.12保留2位小数
    } else {
        return num = null
    }
}

export const floatFix = (num, fixLen = 2) => {
	if (num == 0) {
		return num.toFixed(fixLen)
	}
	if (num) {
		let n = typeof num == "string" ? parseFloat(num) : num  //判断是否是字符串如果是字符串转成数字
		n = n.toFixed(fixLen)  //保留n位
		return n
	} else {
		return num
	}
}

export const intFix = (num) => {
	if (num == 0) {
		return num
	}
	if (num) {
		let n = typeof num == "string" ? parseFloat(num) : num  //判断是否是字符串如果是字符串转成数字
		n = n.toFixed(0)  //保留0位
		return n
	} else {
		return num
	}
}

export default {
	deepClone,
	generateId,
	isEmpty,
	filterJsonByKey,
	copyJsonKey,
	getTextWidth,
	getWeek,
	toMoney,
	floatFix,
	intFix,
}